//
//  YYEEmoticonKeyboard.swift
//  weibo14
//
//  Created by mac on 16/3/13.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit



class YYEEmoticonKeyboard: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
    
        // 底部工具栏
        addSubview(toolbar)
        
        toolbar.snp_makeConstraints { (make) -> Void in
            
            make.trailing.bottom.leading.equalTo(self)

        }
        
        // 表情栏
        addSubview(collectionView)
        
        collectionView.snp_makeConstraints { (make) -> Void in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(toolbar.snp_top)
        }
        
        // pageController
        addSubview(pageController)
        pageController.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.bottom.equalTo(collectionView)
        }

        
        // 主队列异步  相当于最后跑的程序
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            // 让collectionView滚动到第1组
            let indexPath = NSIndexPath(forItem: 0, inSection: 1)
            self.collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false)
            // 去选中对应的表情按钮
            self.toolbar.selectedButtonWithSection(indexPath.section)
        }

        
        
    }
    
    
    // 通过 indexPath 设置 pageControl当前显示的总页数和当前选中的页
    private func setupPageControl(indexPath: NSIndexPath) {
        pageController.numberOfPages = YYEEmoticonTools.shareEmoticonTools.allEmoticons[indexPath.section].count
        pageController.currentPage = indexPath.item
        // 当是第0组的时候,就隐藏pageControl
        pageController.hidden = indexPath.section == 0
    }
    
    
    // MARK: 懒加载控件
    
    private lazy var toolbar:YYEEmoticonToolbar = {
        let vc = YYEEmoticonToolbar(frame: CGRectZero)
        vc.delegate = self
        return vc
    }()
    
    private lazy var collectionView:YYEEmoticonCollectionView = {
        let v = YYEEmoticonCollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
        
        v.delegate = self
        v.dataSource = self
        
        v.registerClass(YYEEmoticonCollectionCell.self, forCellWithReuseIdentifier: "YYEEmoticonCollectionCell")
        return v
    }()
    
    private lazy var pageController:UIPageControl = {
    
        let v = UIPageControl()
        
        v.userInteractionEnabled = false
        
        v.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "_currentPageImage")
        v.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "_pageImage")


        return v
    }()
    
}

extension YYEEmoticonKeyboard:UICollectionViewDataSource,UICollectionViewDelegate {
    
    
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        
        return YYEEmoticonTools.shareEmoticonTools.allEmoticons.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    
        return YYEEmoticonTools.shareEmoticonTools.allEmoticons[section].count
    }


    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("YYEEmoticonCollectionCell", forIndexPath: indexPath) as! YYEEmoticonCollectionCell
        
        cell.indexPath = indexPath
        
        cell.emoticons = YYEEmoticonTools.shareEmoticonTools.allEmoticons[indexPath.section][indexPath.item]
        
        // 一样的
        // print("\(indexPath.item)---\(indexPath.row)")
        
        return cell
    }
    
    // 监听 collectionView的滚动
    func scrollViewDidScroll(scrollView: UIScrollView) {
    
        let cells = collectionView.visibleCells()
        
        if cells.count == 2{
        
            // 去到两个cell
            let firstCell = cells.first!
            let lastCell = cells.last!
            
            // 判断两个cell哪一个在界面上显示得多
            let firstResult = abs(firstCell.frame.origin.x - collectionView.contentOffset.x)
            let lastReulst = abs(lastCell.frame.origin.x - collectionView.contentOffset.x)

            let cell = firstResult < lastReulst ? firstCell : lastCell
            // 取到该cell对应的indexPath
            let indexPath = collectionView.indexPathForCell(cell)!

            // 去选中对应的表情按钮
            toolbar.selectedButtonWithSection(indexPath.section)

            setupPageControl(indexPath)
        }
    }

}






extension YYEEmoticonKeyboard:YYEEmoticonToolbarDelegate {

    func emoticonToolar(toolBar: YYEEmoticonToolbar, type emotionTypeSelected: YYEEmotionType) {
    
        // 将collectionView滚动到指定的组就可以了
        let indexPath = NSIndexPath(forItem: 0, inSection: emotionTypeSelected.rawValue)
        // 让collectionView滚动到指定的indexPath
        collectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false)
        
        setupPageControl(indexPath)
    }

}







